package com.kh.pajx.sop.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.ISchoolDao;
import com.kh.pajx.sop.dao.ISchoolRelationDao;
import com.kh.pajx.sop.dao.school.IBossEcinfoDao;
import com.kh.pajx.sop.dao.sys.ISysAreaDao;
import com.kh.pajx.sop.dao.user.ISysInsideUserDao;
import com.kh.pajx.sop.domain.BossEcinfo;
import com.kh.pajx.sop.domain.HsSchoolInfo;
import com.kh.pajx.sop.domain.HsSchoolRelation;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.domain.SysArea;
import com.kh.pajx.sop.domain.SysInsideUser;
import com.kh.pajx.sop.service.ISchoolService;
import com.kh.pajx.sop.util.page.QueryHelper;
import com.kh.pajx.sop.util.page.QueryHelperForManyTable;

/**
 * 学校信息业务实现类
 * 
 * @author: 徐江涛 2014年6月23
 */
@Service
@Transactional
public class SchoolServiceImpl extends BaseServiceImpl<HsSchoolInfo> implements
		ISchoolService {
	@Resource
	private ISchoolDao schoolDao;// 注入学校操作Dao

	@Resource
	private ISchoolRelationDao schoolrelationDao;// 注入学校关系操作Dao

	/*
	 * azzcsimp更改
	 * @Resource
	private InsideUserDao insideUserDao;// 注入内部用户Dao，用于查询与学校关联的销售人员，客服人员等
*/
	@Resource
	private ISysInsideUserDao insideUserDao;
	
	@Resource
	private ISysAreaDao areaDao;// 注入区域Dao

	/*@Resource
	private OutsideUserDao outsideUserDao;// 注入外部用户Dao
*/
	@Resource
	private IBossEcinfoDao bossEcinfoDao;// 注入集团客户Dao

	/**
	 * 查询所有学校
	 */
	@Override
	public List<HsSchoolInfo> findAllSchool() {
		return schoolDao.findAllSchool();
	}

	@Override
	public HsSchoolInfo getSchoolById(String sclid) {
		return schoolDao.getSchoolById(sclid);
	}

	@Override
	public PageBean getPageBean(int pageNum, int pageSize,
			QueryHelper queryHelper) {
		return schoolDao.getPageBean(pageNum, pageSize, queryHelper);
	}

	/**
	 * 根据学校编号查询与之关联的学校关系
	 * 
	 * @param sclid
	 *            学校编号
	 * @author xujiangtao
	 */
	@Override
	public HsSchoolRelation getSchoolRelationBySclId(String sclid) {
		return schoolrelationDao.getSchoolRelationBySclid(sclid);
	}

	/**
	 * 根据学校关系中的销售人员和客服人员id查询与之关联的销售人员，客服人员等
	 * 
	 * @param sclid
	 *            学校编号
	 * @author xujiangtao
	 */
	@Override
	public SysInsideUser getUserBySclId(String uuid) {
		return insideUserDao.getById(uuid);
	}

	/**
	 * 根据学校的地区id查询地区
	 * 
	 * @param areaCode
	 *            区域id
	 * @author xujiangtao
	 */
	@Override
	public SysArea getAreaByCode(String areaCode) {
		return areaDao.getAreaByCode(areaCode);
	}

	/**
	 * 查询所有地区
	 * 
	 * @param
	 * @author xujiangtao
	 */
	@Override
	public List findAllArea() {
		return areaDao.findAll();
	}

	/**
	 * 修改学校信息
	 * 
	 * @param school
	 *            学校基本信息, schoolRelation 学校关系信息
	 * @author xujiangtao
	 */
	@Override
	public boolean updateSchoolInfo(HsSchoolInfo school,
			HsSchoolRelation schoolRelation) {
		boolean flag = false;
		try {
			schoolDao.update(school);
			schoolrelationDao.save(schoolRelation);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;

	}

	/**
	 * 添加学校信息.关系信息
	 * 
	 * @param hsSchoolInfo
	 * @param hsSchoolRelation
	 * @return
	 * @throws Exception
	 * @author lipengfei 2014/6/25
	 */
	public boolean addSchool(HsSchoolInfo hsSchoolInfo,
			HsSchoolRelation hsSchoolRelation) throws Exception {
		// 获取电话号码和联系人电话号码
		hsSchoolInfo.setSclPhone(hsSchoolInfo.getSclPhone().replace("-", ""));
		hsSchoolInfo.setSclLeaderPhone(hsSchoolInfo.getSclLeaderPhone()
				.replace("-", ""));
		// 添加学校信息
		boolean add = schoolDao.addSchool(hsSchoolInfo);
		// 添加学校编号
		hsSchoolRelation.setSclId(hsSchoolInfo.getSclId());
		if (add) {
			// 格式化当前时间。并转换为字符窜格式；
			java.util.Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			hsSchoolRelation.setCreateTime(sdf.format(date));
			// 添加学校扩展关系信息
			schoolrelationDao.save(hsSchoolRelation);
			return true;
		}
		return false;
	}

	/**
	 * 根据学校关系中学校管理员id，移动客户经理id查询与之关联的，客服人员等
	 * 
	 * @param uuid
	 *            学校关系中的对应人员id
	 * @author xujiangtao
	 */

	/*@Override
	public OutsideUser getOutUserById(String uuid) {
		// TODO Auto-generated method stub
		return outsideUserDao.getById(uuid);
	}*/

	@Override
	public PageBean getPageBean(int pageNum, int pageSize,
			QueryHelperForManyTable queryHelperFromMany) {
		return schoolDao.getPageBean(pageNum, pageSize, queryHelperFromMany);
	}

	@Override
	public List findByHql(String hql) {
		return schoolDao.findByHql(hql);
	}

	@Override
	public BossEcinfo getEcByEcId(String ecid) {
		return bossEcinfoDao.getBossEcinfoByEcId(ecid);
	}

	@Override
	public List<BossEcinfo> findAllBossEcinfo() {
		// TODO Auto-generated method stub
		return bossEcinfoDao.findAll();
	}

	@Override
	public boolean updateSchoolRelation(HsSchoolRelation schoolRelation) {
		boolean flag = false;
		try {
			schoolrelationDao.update(schoolRelation);
			flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 查询方法
	 * 
	 * @author taller 2014/7/9
	 */
	public List<HsSchoolInfo> searchSchool(Map<String, String> params) {
		String school = params.get("school");
		StringBuilder sb = new StringBuilder();
		sb.append("select school from HsSchoolInfo school where 1=1 ");
		if (StringUtils.isNotEmpty(school)) {
			sb.append(" and school.school='").append(school).append("' ");
		}
		return schoolDao.findByHql(sb.toString());
	}

	@Override
	public boolean updateSchool(HsSchoolInfo school,
			HsSchoolRelation schoolRelation) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			schoolDao.update(school);
			schoolrelationDao.update(schoolRelation);
			flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 根据地区编码自动获取学校代号
	 * 
	 * @author lipengfei2014/7/9
	 */
	public String getSclNumber(String area) {
		String hql = "select scl from HsSchoolInfo scl where scl.sclNumber like'"
				+ area + "%'";
		List<HsSchoolInfo> schoolInfo = schoolDao.findList(hql);
		List<String> number = new ArrayList<String>();
		for (int i = 0; i < schoolInfo.size(); i++) {
			number.add(schoolInfo.get(i).getSclNumber().substring(2));
		}
		Collections.sort(number);
		return number.get(number.size() - 1);
	}

	@Override
	public List<HsSchoolInfo> getSclBySclIds(List<String> list) {
		// TODO Auto-generated method stub
		return schoolDao.getSchoolBySclIds(list);
	}
	@Override
	/**查询各地区开通学校数量*/
	public List getSclCountByArea() {
		
		return schoolDao.getSclCountByArea();
	}
	/**统计在校生、毕业生开通人数*/
	@Override
	public List getstuInOutOpenNum(){
		
		return schoolDao.getstuInOutOpenNum();
	}
	/**--统计在校生各套餐分布情况*/
	public List gettaocanNum(){
		
		return schoolDao.gettaocanNum();
	}
	/**在校生人数、在校生开通人数、毕业生人数、毕业生开通人数 */
	public List getstuAllNum(){
		
		return schoolDao.getstuAllNum();
	}
	/**各部门截止当前开通人数*/
	public List getdeptOpenNum(){
		
		return schoolDao.getdeptOpenNum();
	}
	
	
	
	
	@Override
	/**查询市场各部门开通学校数量*/
	public List getSclCountByDept() {
		
		return schoolDao.getSclCountByDept();
	}
	/**按类型查询设备数量*/
	@Override
	public int getEquCount(String type) {
		// TODO Auto-generated method stub
		return schoolDao.getEquCount(type);
	}
	
	@Override
	public HsSchoolInfo findByName(String sclName) {
		// TODO Auto-generated method stub
		return schoolDao.findByName(sclName);
	}

}
